package com.chen;

import com.chen.pageProcessor.DemoProcessor;
import com.chen.pageProcessor.EntranceProcessor;
import com.chen.pageProcessor.ItemProcessor;
import com.chen.pageProcessor.TypeProcessor;
import com.chen.pipeline.JDItemPipeline;


import com.chen.proxyProvider.UpdateProxyProvider;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import com.chen.scheduler.RedisJDScheduler;

import java.io.*;
import java.util.*;

/**
 * @author 陈俊宏
 */
public class Main {
    public static void main(String[] args) {
        Properties properties = null;
        try {
            InputStream inputStream = new FileInputStream(new File("./src/setting.properties"));
            properties = new Properties();
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        Request request = new Request("https://www.jd.com/allSort.aspx");
        request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        request.addHeader("Accept-Encoding", "gzip, deflate, br");
        request.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        request.addHeader("Connection", "keep-alive");
        request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0");
        request.addHeader("Host", "www.jd.com");
        request.addHeader("Pragma", "no-cache");
        request.setPriority(10);


        JedisPoolConfig config = new JedisPoolConfig();

        //最大空闲数量
        config.setMaxIdle(1);
        //连接最多数量
        config.setMaxTotal(2);
        //空闲时检查有效性
        config.setTestWhileIdle(true);
        //获取连接时检查有效性
        config.setTestOnBorrow(true);
        assert properties != null;
        JedisPool allinfo = new JedisPool(config, properties.getProperty("host"), 6379, 10000, properties.getProperty("password"));
        JedisPool control = new JedisPool(config, "127.0.0.1", 6379, 10000, "chen311696");

        int threadNumber = Integer.parseInt(properties.getProperty("ThreadNumber"));
        int proxyNumber = Integer.parseInt(properties.getProperty("ProxyNumber"));
        HttpClientDownloader httpClientDownloader = new HttpClientDownloader();

        httpClientDownloader.setProxyProvider(new UpdateProxyProvider(proxyNumber, properties));

        Spider spider = Spider.create(new EntranceProcessor(new TypeProcessor(new DemoProcessor(new ItemProcessor())), properties));
        spider.setDownloader(httpClientDownloader)
                .addRequest(request)
                .addPipeline(JDItemPipeline.getInstance(allinfo))
                .thread(threadNumber)
                .setUUID("bigData")
                .setScheduler(new RedisJDScheduler(control))
                .start();

        while (true) {
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            if ("p".equals(input)) {
                spider.stop();
            } else if ("start".equals(input)) {
                spider.start();
            } else if (input.startsWith("set")) {
                String[] s = input.split(" ");
                if (s.length == 3) {
                    properties.setProperty(s[1], s[2]);
                    Set<Map.Entry<Object, Object>> entries = properties.entrySet();
                    Iterator<Map.Entry<Object, Object>> iterator = entries.iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<Object, Object> next = iterator.next();
                        System.out.println(next);
                    }
                } else {
                    System.out.println("参数数量不正确");
                }
            } else if ("info".equals(input)) {
                Set<Map.Entry<Object, Object>> entries = properties.entrySet();
                Iterator<Map.Entry<Object, Object>> iterator = entries.iterator();
                while (iterator.hasNext()) {
                    Map.Entry<Object, Object> next = iterator.next();
                    System.out.println(next);
                }
            } else {
                System.out.println("无效指令");
            }
        }
    }
}
